package com.ray.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.EquipmentBean;
import com.ray.bean.OrderBean;
import com.ray.bean.ProcedureBean;
import com.ray.bean.ReportBean;
import com.ray.bean.WeiXinPeopleBean;
import com.ray.bean.WorkshopProductionCycleBean;
import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;
import com.ray.pojo.message.send.Text;
import com.ray.pojo.message.send.TextMessage;
import com.ray.service.Contacts_DepartmentService;
import com.ray.service.Contacts_UserService;
import com.ray.service.EquipmentService;
import com.ray.service.OrderService;
import com.ray.service.ProcedureService;
import com.ray.service.ReturnWorkService;
import com.ray.service.SendMessageService;
import com.ray.service.UserService;
import com.ray.service.WeiXinPeopleService;
import com.ray.service.WeiXinService;
import com.ray.service.WorkOrderService;
import com.ray.service.WorkshopService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;
import com.ray.util.WeiXinParamesUtil;
import com.ray.util.WeiXinUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("weiXin")
public class WeiXinController {
	
	@Autowired
	Contacts_DepartmentService contacts_DepartmentService;
	@Autowired
	Contacts_UserService contacts_UserService;
	@Autowired
	WeiXinService weiXinService;
	@Autowired
	WorkshopService workshopService;
	@Autowired
	ProcedureService procedureService;
	@Autowired
	OrderService orderService;
	@Autowired
	ReturnWorkService returnWorkService;
	@Autowired
	WeiXinPeopleService weiXinPeopleService;
	@Autowired
	UserService userService;
	@Autowired
	WorkOrderService workOrderService;
	@Autowired
	EquipmentService equipmentService;
	

//	//获取微信扫一扫sdk配置参数
//	@RequestMapping("/getWeiXinConfig")
//	@ResponseBody
//	public Map getWeiXinConfig(HttpServletRequest request,String userName) throws ServiceException, UnsupportedEncodingException {
//		Object object=request.getSession().getAttribute("beforeTime");
//		Long beforeTime=(long) 0;
//		String ticket=(String) request.getSession().getAttribute("ticket");
//		if(null!=object) {
//			beforeTime=(Long)object;
//		}
//		Long jianGe=(System.currentTimeMillis()-beforeTime)/1000;
//		if(jianGe>6000) {
//			ticket=WeiXinUtil.getTicket();
//			request.getSession().setAttribute("beforeTime", System.currentTimeMillis());
//			request.getSession().setAttribute("ticket", ticket);
//		}
//		String url=WeiXinParamesUtil.baoGongUrl+URLEncoder.encode(userName,"utf-8");
//		Map map=weiXinService.getConfig(ticket,userName,url);
//		return map;
//	}
	
	
	@RequestMapping("/getWeiXinGroup")
	@ResponseBody
	public JSONObject getWeiXinGroup() {
		String departmentId="0";
		 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpID, WeiXinParamesUtil.workSecret).getToken();
        JSONObject xx=contacts_DepartmentService.getDepartmentList(accessToken, departmentId);
        return  xx;
	}
	
	@RequestMapping("/addSend")
	@ResponseBody
	public Map addSend(String stringId) {
		weiXinService.addSend(stringId);
		return  ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
	@RequestMapping("/getSendPeople")
	@ResponseBody
	public Map getSendPeople(String dapartmentId) {
		return  ResponseUtil.ResponseResult(weiXinService.getSendPeople(dapartmentId));
	}
	
	@RequestMapping("/getWeiXinGroupPeople")
	@ResponseBody
	public JSONObject getWeiXinGroupPeople(String deptId) {
		System.out.println(deptId+"*************************************");
		String fetchChild="1";
		//2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
		String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpID, WeiXinParamesUtil.addressSecret).getToken();
		JSONObject xx=contacts_UserService.getDepartmentUser(accessToken, deptId, fetchChild);
		return  xx;
	}
	
	@RequestMapping("/getClock")
	@ResponseBody
	public Map getClock() {
		return  ResponseUtil.ResponseResult(weiXinService.getClock());
	}
	
	@RequestMapping("/sendMessage")
	@ResponseBody
	public JSONObject sendMessage() {
		 //0.设置消息内容
        String content="Dear all，有一个产能异常\n" +
                "<a href=\"http://2c5n3u.natappfree.cc/weixin/queryCapacity.html\">点此查看" +
                "</a>";
        //1.创建文本消息对象
        TextMessage message=new TextMessage();
        String touser=weiXinService.getTouser();
        System.out.println(touser);
        message.setTouser(touser);  //不区分大小写
        message.setMsgtype("text");
        message.setAgentid(WeiXinParamesUtil.workAgentId);

        Text text=new Text();
        text.setContent(content);
        message.setText(text);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpID, WeiXinParamesUtil.workSecret).getToken();
        System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms=new SendMessageService();
		return sms.sendMessage(accessToken, message);
	}
	
	//获取微信扫一扫sdk配置参数
	@RequestMapping("/getConfig")
	@ResponseBody
	public Map getConfig(HttpServletRequest request,String userName) throws ServiceException, UnsupportedEncodingException {
        String ticket=weiXinService.getTiket(request);
		String url=WeiXinParamesUtil.baoGongUrl+URLEncoder.encode(userName,"utf-8");
		Map map=weiXinService.getConfig(ticket,userName,url);
		return map;
	}
	
	
	@RequestMapping(value="/saoYiSaoTest" , method = RequestMethod.GET)
	public String testGetCode(String code,String state,Model model,HttpServletRequest request) throws ServiceException {
		String userName=weiXinService.getDelayWorkUser(code,request);
		model.addAttribute("userName", userName);
		return "redirect:/saoYiSaoTest.jsp";
	}
	
//	获取报工授权url
	@ResponseBody
	@RequestMapping("/getCodeUrl")
	public String getCodeUrl() throws UnsupportedEncodingException {
		String url= WeiXinUtil.getCodeUrl(WeiXinParamesUtil.baoGongReditUrl);
	    System.out.println(url);
		return url;
	}
	
	
//	获取微信公众号静默授权url
//	@ResponseBody
	@RequestMapping("/sh")
	public String  sh(String orderId,HttpServletRequest request) throws UnsupportedEncodingException {
		String url= WeiXinUtil.getWeiXinPubliucNumberCodeUrl(WeiXinParamesUtil.weiXinPublicNumberReportController+orderId);
		System.out.println(url);
		return "redirect:"+url;
	}
	
	
	
	
//    重定向到微信公众号报工页面
	@RequestMapping(value="/weiXinPublicNumberReport" , method = RequestMethod.GET)
	public String weiXinPublicNumberReport(String orderId,String code,String state,Model model,HttpServletRequest request,HttpServletResponse response) throws ServiceException, IOException {
		if(orderId==null || "".equals(orderId)) {
			JSONObject jsonObject=weiXinService.getOpenId(code);
			String openId=jsonObject.getString("openid");
			String unionId=jsonObject.has("unionid")?jsonObject.getString("unionid"):"";
			model.addAttribute("weiXinPublicOpenId",openId);
			model.addAttribute("weiXinPublicunionId",unionId);
			return "redirect:/login.jsp";
		}
		
		
		OrderBean orderBean=orderService.getOrderByOrderId(orderId);
		Assert.notNull(orderBean,"该订单不存在");
		MyUser myUser=weiXinService.getWeiXinPublicNumberReportUser(code,request,orderBean.getCompany(),"/saoYiSaoTest.jsp");
		if(orderId!=null && !"".equals(orderId)) {
			if(null==myUser.getUserName()||"".equals(myUser.getUserName())) {
				model.addAttribute("weiXinPublicOpenId", myUser.getWeiXinPublicOpenId());
				model.addAttribute("weiXinPublicunionId",myUser.getUnionId());
				return "redirect:/login.jsp";
			}else {
				model.addAttribute("userName", myUser.getUserName());
				model.addAttribute("orderId", orderId);
				return "redirect:/saoYiSaoTest.jsp";
			}
		}else {
		    	model.addAttribute("weiXinPublicunionId",myUser.getUnionId());
				model.addAttribute("weiXinPublicOpenId", myUser.getWeiXinPublicOpenId());
				return "redirect:/login.jsp";
		}
	}
	
	//重定向到workshopProductionCycle.jsp
	@RequestMapping(value="/workshopProductionCycle" , method = RequestMethod.GET)
	public String workshopProductionCycleGetCode(String code,String state,Model model,HttpServletRequest request) throws ServiceException {
		String userName=weiXinService.getDelayWorkUser(code,request);
		model.addAttribute("userName", userName);
		return "redirect:/workshopProductionCycle.jsp";
	}
	
	//获取重定向url
	@ResponseBody
	@RequestMapping("/getCycleUrl")
	public String getCycleUrl() throws UnsupportedEncodingException {
		String url= WeiXinUtil.getCodeUrl(WeiXinParamesUtil.workshopProductionCycleRedirectUrl);
	    System.out.println(url);
		return url;
	}
	
	//重定向到processTable.jsp
	@RequestMapping(value="/processTable" , method = RequestMethod.GET)
	public String processTableGetCode(String code,String state,Model model,HttpServletRequest request) throws ServiceException {
		String userName=weiXinService.getDelayWorkUser(code,request);
		model.addAttribute("userName", userName);
		return "redirect:/processTable.jsp";
	}
	
	//获取重定向url
	@ResponseBody
	@RequestMapping("/getProcessTableUrl")
	public String getProcecssTableUrl() throws UnsupportedEncodingException {
		String url= WeiXinUtil.getCodeUrl(WeiXinParamesUtil.processTableRedirectUrl);
	    System.out.println("getProcessTableUrl:"+url);
		return url;
	}
	
	//重定向到customers.jsp
	@RequestMapping(value="/customers" , method = RequestMethod.GET)
	public String customersGetCode(String code,String state,Model model,HttpServletRequest request) throws ServiceException {
		String userName=weiXinService.getDelayWorkUser(code,request);
		model.addAttribute("userName", userName);
		return "redirect:/customers.jsp";
	}
	
	//获取重定向url
	@ResponseBody
	@RequestMapping("/getCustomersUrl")
	public String getCustomersUrl() throws UnsupportedEncodingException {
		String url= WeiXinUtil.getCodeUrl(WeiXinParamesUtil.customersRedirectUrl);
	    System.out.println("getCustomersUrl:"+url);
		return url;
	}
	
	@ResponseBody
	@RequestMapping("/getShenHe")
	public String getShenHe() throws UnsupportedEncodingException {
		String url= WeiXinUtil.getCodeUrl(WeiXinParamesUtil.shenHeReditUrl);
		System.out.println(url);
		return url;
	}
	
	//短出重定向页面
	@ResponseBody
	@RequestMapping("/getDuanChuUrl")
	public String getDuanChuUrl() throws UnsupportedEncodingException {
		String url= WeiXinUtil.getCodeUrl(WeiXinParamesUtil.duanChuReditUrl);
		System.out.println(url);
		return url;
	}
	
	@RequestMapping(value="/getDuanChuPage" , method = RequestMethod.GET)
	public String getDuanChuPage(String code,String state,Model model,HttpServletRequest request) throws ServiceException {
		String userName=weiXinService.getDelayWorkUser(code,request);
		model.addAttribute("userName", userName);
		return "redirect:/duanChu.jsp";
	}
	
	//获取微信扫一扫sdk配置参数
	@RequestMapping("/getDuanChuConfig")
	@ResponseBody
	public Map getDuanChuConfig(HttpServletRequest request,String userName) throws ServiceException, UnsupportedEncodingException {
		Object object=request.getSession().getAttribute("beforeTime");
		Long beforeTime=(long) 0;
		String ticket=(String) request.getSession().getAttribute("ticket");
		if(null!=object) {
			beforeTime=(Long)object;
		}
		Long jianGe=(System.currentTimeMillis()-beforeTime)/1000;
		if(jianGe>6000) {
			ticket=WeiXinUtil.getTicket();
			request.getSession().setAttribute("beforeTime", System.currentTimeMillis());
			request.getSession().setAttribute("ticket", ticket);
		}
		String url=WeiXinParamesUtil.duanChuUrl+URLEncoder.encode(userName,"utf-8");
		Map map=weiXinService.getConfig(ticket,userName,url);
		return map;
	}
	
	@RequestMapping(value="/getShenHePage" , method = RequestMethod.GET)
	public String getShenHe(String code,String state,Model model,HttpServletRequest request) throws ServiceException {
		String userName=weiXinService.getDelayWorkUser(code,request);
		model.addAttribute("userName", userName);
		return "redirect:/shenHe.jsp";
	}
	
	
   //添加工序
	@RequestMapping("/addProcedur")
	@ResponseBody
	public Map addProcedur(ProcedureBean procedureBean) {
		procedureService.addProcedur(procedureBean);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
	//查询每个车间七天内拖期订单数
	@RequestMapping("/insertReport")
	@ResponseBody
	public Map insertReport(String  reportArrayJSONStr,HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		MyUser myUser=RequestUtil.getLoginUser(request);
		return ResponseUtil.ResponseResult(workshopService.insertReport(reportArrayJSONStr,null,myUser));
	}
	
//	获取订单总数
	@RequestMapping("/getOrderDetail")
	@ResponseBody
	public Map getOrderDetail(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		List<OrderBean> order=orderService.getOrderDetail(map);
		//如果订单是多部门订单
		if("多部门".equals(order.get(0).getFrameType())) {
			order.get(0).setDeptId(map.get("loginUserDeptId").toString());
		}else {
			if(!map.get("loginUserDeptId").toString().equals(order.get(0).getDeptId())){
				List<String> otherDept=orderService.checkOtherDept(map);
				if(null==otherDept || otherDept.size()<=0) {
					throw new ServiceException("订单部门与报工人部门不一致，不可报工！");
				}
			}
		}
		return ResponseUtil.ResponseResult(order);
	}
	
	
//	获取订单总数
	@RequestMapping("/getOrderList")
	@ResponseBody
	public Map getOrderList(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		List<OrderBean> order=orderService.getOrderDetail(map);
		return ResponseUtil.ResponseResult(order);
	}
	
//	获取所有车间名称
	@RequestMapping("/getWorkshopName")
	@ResponseBody
	public Map getWorkshopName(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getWorkshopName(map));
	}

//	获取该车间下所有工序名称
	@RequestMapping("/getWorkingProcedureName")
	@ResponseBody
	public Map getWorkingProcedureName(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getWorkingProcedureName(map));
	}
	
//	获取订单颜色
	@RequestMapping("/getColor")
	@ResponseBody
	//orderId(订单编号) machining(加工对象文本)  reportMachining(报工对象) isAllMaterial(是否是交接工序) machiningValue(加工对象值) workshopNameValue(车间id) procedureVal(工序id) procedureText(工序名称)
	public Map getColor(String orderId,String machining,String reportMachining,int isAllMaterial,String machiningValue,String workshopNameValue,String procedureVal,String procedureText,HttpServletRequest request) throws ServiceException {
		MyUser myUser=RequestUtil.getLoginUser(request);
		return ResponseUtil.ResponseResult(orderService.getColor(orderId,machining,myUser.getCompany(),reportMachining,isAllMaterial,machiningValue,workshopNameValue,procedureVal,procedureText));
	}
	
	//获取不合格原因
	@RequestMapping("/getNoReason")
	@ResponseBody
	public Map getNoReason(String workShopName,String produce) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getNoReason(workShopName,produce));
	}
	
//	获取订单分颜色投料数
	@RequestMapping("/getOrderFieeding")
	@ResponseBody
	public Map getOrderDetail1(ReportBean reportBean) throws ServiceException {
		return ResponseUtil.ResponseResult(orderService.getOrderFieeding(reportBean));
	}
	
	//获取厂家
	@RequestMapping("/getFactory")
	@ResponseBody
	public Map getFactory(String workShopName) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getFactory(workShopName));
	}
	
	//获取设备编号
	@RequestMapping("/getDeviceId")
	@ResponseBody
	public Map getDeviceId(String workShopName) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getDeviceId(workShopName));
	}
	
	//获取设备名称
	@RequestMapping("/getDeviceName")
	@ResponseBody
	public Map getDeviceName(String workShopName) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getDeviceName(workShopName));
	}
	
	//获取返修报工厂家
	@RequestMapping("/getFactoryReturn")
	@ResponseBody
	public Map getFactoryReturn(String workShopName) throws ServiceException{
		return ResponseUtil.ResponseResult(returnWorkService.getFactoryReturn(workShopName));
	}
	
	//获取返修报工设备编号
	@RequestMapping("/getDeviceIdReturn")
	@ResponseBody
	public Map getDeviceIdReturn(String workShopName) throws ServiceException{
		return ResponseUtil.ResponseResult(returnWorkService.getDeviceIdReturn(workShopName));
	}
	
	//获取返修报工设备名称
	@RequestMapping("/getDeviceNameReturn")
	@ResponseBody
	public Map getDeviceNameReturn(String workShopName) throws ServiceException{
		return ResponseUtil.ResponseResult(returnWorkService.getDeviceNameReturn(workShopName));
	}

	//审核删除订单
//	@RequestMapping("/deleteOder")
//	@ResponseBody
//	public Map deleteOder(String id) throws ServiceException{
//		workshopService.deleteOder(id);
//		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
//	}
	
	//审核订单
//	@RequestMapping("/shenHeOrder")
//	@ResponseBody
//	public Map shenHeOrder(String userName,String id) throws ServiceException{
//		workshopService.shenHeOrder(userName,id);
//		return ResponseUtil.ResponseResult(ReturnStringUtil.APPROVAL_SUCCESS);
//	}
	
	//获取审核订单
	@RequestMapping("/getShenHeOrder")
	@ResponseBody
	public Map getShenHeOrder(String userName) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getShenHeOrder(userName));
	}
	
	
	//获取订单详情每个框色订单数
	@RequestMapping("/getColorCount")
	@ResponseBody
	public Map getColorCount(String orderId) throws ServiceException{
		return ResponseUtil.ResponseResult(orderService.getColorCount(orderId));
	}
	
	
	//设置可短数
	@RequestMapping("/setDuanChu")
	@ResponseBody
	public Map setDuanChu(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		orderService.setDuanChu(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
	
	//获取用户所在你部门
	@RequestMapping("/getUserWorkShop")
	@ResponseBody
	public Map getUserWorkShop(String userName) throws ServiceException{
		return ResponseUtil.ResponseResult(weiXinService.getUserWorkShop(userName));
	}
	
	//校验部门是否为生产部门
	@RequestMapping("/checkWorkShop")
	@ResponseBody
	public Map checkWorkShop(String workShop) throws ServiceException{
		return ResponseUtil.ResponseResult(procedureService.checkWorkShop(workShop));
	}
	
	//获取合格数
	@RequestMapping("/getqualified")
	@ResponseBody
	public Map getqualified(ReportBean reportBean) throws ServiceException{
		return ResponseUtil.ResponseResult(workshopService.getqualified(reportBean));
	}
	
	//插入返修数据
	@RequestMapping("/insertReturnWork")
	@ResponseBody
	public Map insertReturnWork(String reportArrayJSONStr) throws ServiceException{
		returnWorkService.insertReturnWork(reportArrayJSONStr);
		return ResponseUtil.ResponseResult(ReturnStringUtil.RETURN_REPORT_SUCCESS);
	}
	
	//插入返修数据
	@RequestMapping("/returnWorkReport")
	@ResponseBody
	public Map returnWorkReport(ReportBean reportBean) throws ServiceException, CloneNotSupportedException{
		returnWorkService.returnWorkReport(reportBean);
		return ResponseUtil.ResponseResult(ReturnStringUtil.RETURN_REPORT_SUCCESS);
	}
	
	//插入返修数据
	@RequestMapping("/returnWorkColor")
	@ResponseBody
	public Map returnWorkColor(String machining,String orderId) throws ServiceException{
		return ResponseUtil.ResponseResult(returnWorkService.returnWorkColor(machining,orderId));
	}
	
	
	//订单物料类型
	@RequestMapping("/getOrderMeType")
	@ResponseBody
	public Map getOrderMeType(String orderId,String workshopName,int isAllMaterial,String procedureId,String procedureText,HttpServletRequest request) throws ServiceException{
		MyUser myUser=RequestUtil.getLoginUser(request);
		return ResponseUtil.ResponseResult(orderService.getOrderMeType(orderId,workshopName,myUser.getCompany(),isAllMaterial,procedureId,procedureText));
	}
	
	//移除微信用户绑定
	@RequestMapping("/removeBindUser")
	public String  removeBindUser(HttpServletRequest request,Model model) throws ServiceException{
		//获取登录用户
		Map map=RequestUtil.getRequestAllParam(request);
		List<String> userIdList=new ArrayList<String>();
		userIdList.add(map.get("loginUserId").toString());
		List<WeiXinPeopleBean> oepnIdList=weiXinPeopleService.getWeiXinPeopleByUserIdList(userIdList);
		weiXinPeopleService.deleteWeiXinPeopleByUserId(map);
		request.getSession().setAttribute("userLogin", null);
		model.addAttribute("weiXinPublicOpenId", oepnIdList.get(0).getOpenid());
		model.addAttribute("weiXinPublicunionId",oepnIdList.get(0).getUnionid());
		return "redirect:../login.jsp";
	}
	
	//微信公众号jssdk
	@RequestMapping("/getConfigJsSDK")
	@ResponseBody
	public Map<String, String>  getConfigJsSDK(String url) throws UnsupportedEncodingException {
		return ResponseUtil.ResponseResult(weiXinService.getConfigJsSDK(url));
	}
	
	/**
	 * 扫描物料网卡二维码
	 * @param url
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/s")
	public String  scannInternetQeCode(String i) throws UnsupportedEncodingException {
		String  redirUrl="/phoneApp/internetThingsCard";
        String  workOrderId=workOrderService.getIdByInternetThingsCard(i);
        String  equimentNo=equipmentService.getEquimentNoByInternetThingsCard(i);
        if((workOrderId!=null && !"".equals(workOrderId)) || (equimentNo!=null && !"".equals(equimentNo))) {
        	redirUrl+="?";
        }
        if(workOrderId!=null && !"".equals(workOrderId)) {
        	redirUrl+="workOrderId="+workOrderId+"&";
        }
        if(equimentNo!=null && !"".equals(equimentNo)) {
        	redirUrl+="equimentNo="+equimentNo+"&";
        }
        if(redirUrl.indexOf('?')>0) {
        	redirUrl=redirUrl.substring(0,redirUrl.length()-1);
        }
		return "redirect:"+redirUrl;
	}
	
	
	@RequestMapping("/changeOrder")
	public String  changeOrder(String equipmentNo,HttpServletRequest request) throws UnsupportedEncodingException {
		Assert.isTrue(equipmentNo != null && !"".equals(equipmentNo),"未获取到设备编号");
    	String url= WeiXinUtil.getWeiXinPubliucNumberCodeUrl(WeiXinParamesUtil.weiXinChangeOrderController+equipmentNo);
		System.out.println(url);
		return "redirect:"+url;
	}
		
		
		
		
//	    重定向到微信公众号报工页面
	@RequestMapping(value="/changeOrderUser" , method = RequestMethod.GET)
	public String changeOrderUser(String equipmentNo,String code,String state,Model model,HttpServletRequest request,HttpServletResponse response) throws ServiceException, IOException {
		Map map=new HashMap();
		map.put("equipmentNo", equipmentNo);
		List<EquipmentBean> equipmentBeans=equipmentService.getEquipmentByDetail(map);
		Assert.isTrue(equipmentBeans.size()>0,"该设备不存在");
		MyUser myUser=weiXinService.getWeiXinPublicNumberReportUser(code,request,equipmentBeans.get(0).getCompany(),"/saoYiSaoTest.jsp");
		if(null==myUser.getUserName()||"".equals(myUser.getUserName())) {
			model.addAttribute("weiXinPublicOpenId", myUser.getWeiXinPublicOpenId());
			model.addAttribute("weiXinPublicunionId",myUser.getUnionId());
			return "redirect:/login.jsp";
		}else {
			return "redirect:/phoneApp/changeOrder?equipmentNo="+equipmentNo;
		}
	}
	    
}

